package com.tianqu.tms.www.transfer.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.transfer.controller.PChangeTableInputResultController;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;
/**
 * 打印变更表
 * @author User
 *
 */

public class PChangeTableInputResultAction extends TmsAction {
	private Logger logger = Logger
			.getLogger(PChangeTableInputResultAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
        TransferAcceptedInfo accept = new TransferAcceptedInfo();
		isToken(mapping, request);
		// 
        TransferRgInputViewHelper view = (TransferRgInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
        List<TransferAcceptedInfo> infoList = accept.getAcceptInfoList(view.getSerial(), "");
        if (infoList != null) {
            if (infoList.size() == 1) {
                accept  = infoList.get(0);
            } else if (infoList.size() == 0) {
                setErrorsToRequest(request, TmsConstants.U0039 ,null);
                return mapping.findForward(TmsConstants.SUCCESS);
            } else {
                setErrorsToRequest(request, TmsConstants.U1405 ,null);
                return mapping.findForward(TmsConstants.SUCCESS);
            }
        } else {
            setErrorsToRequest(request, TmsConstants.U0039 ,null);
            return mapping.findForward(TmsConstants.SUCCESS);
        }
        
        
		PChangeTableInputResultController controller = new PChangeTableInputResultController();
		controller.execute(uc);
        infoList = accept.getAcceptInfoList(view.getSerial(), "");
        if (infoList != null) {
            if (infoList.size() == 1) {
                accept  = infoList.get(0);
            }
        }
		uc.removeAttribute(TmsConstants.VIEWHELP);
//		accept = accept.getAllAcceptInfoBySerial(view.getSerial(),"");
//		TransferAcceptedInfo accept = new TransferAcceptedInfo();
//		accept = accept.getAcceptInfoBySerial(view.getSerial(),"");
		view.setPrintBsflag("1");
		view.setAgentName(accept.getAgentName());

		request.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
